package cn.wizzer.app.web.commons.custom;

import cn.wizzer.app.web.commons.custom.annotation.Custom;
import org.nutz.aop.InterceptorChain;
import org.nutz.aop.MethodInterceptor;
import org.nutz.ioc.Ioc;
import org.nutz.ioc.aop.SimpleAopMaker;
import org.nutz.ioc.loader.annotation.IocBean;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
@IocBean(name="$aop_custom")
public class CustomAopLoader extends SimpleAopMaker<Custom>{


    @Override
    public List<? extends MethodInterceptor> makeIt(Custom custom, Method method, Ioc ioc) {
        return Arrays.asList(new CustomMethodInterceptor());
    }
}

class CustomMethodInterceptor implements MethodInterceptor{

    @Override
    public void filter(InterceptorChain chain) throws Throwable {
        Object[] args = chain.getArgs();

        Method callingMethod = chain.getCallingMethod();
        String methodName = callingMethod.getName();
        Custom annotation = callingMethod.getAnnotation(Custom.class);
        String anno=annotation.toString();
        Class<?> declaringClass = callingMethod.getDeclaringClass();
        String className = declaringClass.getName();

        chain.doChain();
        Object aReturn = chain.getReturn();
        System.out.println("args =" +args[0]);
        System.out.println("return ="+aReturn);
        System.out.println("methodName ="+methodName);
        System.out.println("anno = "+anno);
        System.out.println("className ="+className);

    }
}
